package com.utils;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class BaseResponse<T> {

    /**
     * 状态码，默认成功
     */
    protected Integer code = ResponseStatusCode.SUCCESS.getCode();

    /**
     * 状态描述
     */
    protected String message = "成功";

    private T data;


    public BaseResponse(T data) {
        this.data = data;
    }

    public BaseResponse(int code, String message) {
        this.code = code;
        this.message = message;
    }

    public static <T> BaseResponse<T> ok() {
        return new BaseResponse<>();
    }

    public static <T> BaseResponse<T> ok(T data) {
        return new BaseResponse<>(data);
    }

    public static <T> BaseResponse<T> fail(ResponseStatusCode code, Exception e) {
        return new BaseResponse<>(code.getCode(), e.getMessage());
    }

    public static <T> BaseResponse<T> fail(int code, String message) {
        return new BaseResponse<>(code, message);
    }

    public static <T> BaseResponse<T> fail(ResponseStatusCode code) {
        return new BaseResponse<>(code.getCode(), code.getDesc());
    }

    public static <T> BaseResponse<T> fail(ResponseStatusCode code, String message) {
        return new BaseResponse<>(code.getCode(), message);
    }

    public boolean isSuccess() {
        return this.code == ResponseStatusCode.SUCCESS.getCode();
    }
}